package com.sky.mapper;


import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);

    /**
     *
     * 通过订单号来查询当前订单
     */

    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    void update(Orders orders);


    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 根据状态统计订单数量
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);

    //查询当前的日期内的营业额总和
    @Select("select sum(amount) from orders where order_time > #{beginTime} and order_time < #{endTime} and status = 5")
    Double sumByDate(LocalDateTime beginTime, LocalDateTime endTime);

    //查询当前的日期内的订单总数
    @Select("select count(id) from orders where order_time < #{endTime} and order_time > #{beginTime}")
    Integer counttotalByDate(LocalDateTime beginTime,LocalDateTime endTime);

    //查询当前的日期内的已完成订单总数
    @Select("select count(id) from orders where order_time < #{endTime} and order_time > #{beginTime} and status = 5")
    Integer countUsefulByDate(LocalDateTime beginTime, LocalDateTime endTime);

    //查询当前日期的前10菜品的名字和数量
    @Select("select od.name, sum(od.number)  as number from order_detail od , orders o where od.order_id = o.id and order_time > #{beginTime} and order_time < #{endTime} and o.status = 5 group by od.name order by sum(number) desc limit 10")
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime beginTime, LocalDateTime endTime);

    Integer countByMap(Map map);
}
